package com.jucai.pppeiyouclient.weidget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

/**
 * Created by Kuky on 2017-02-17.
 */

public class SideBar extends View {
    public static String[] str = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
            "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    public int choose = -1;
    private Paint mPaint = new Paint();
    private TextView mTextDialog;
    private OnTouchingLetterChangedListener onTouchingLetterChangedListener;

    public void setTextView(TextView textDialog) {
        this.mTextDialog = textDialog;
    }

    public interface OnTouchingLetterChangedListener {
        void onTouchingLetterChanged(String index);
    }

    public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener letterChangedListener) {
        this.onTouchingLetterChangedListener = letterChangedListener;
    }

    public SideBar(Context context) {
        super(context);
    }

    public SideBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SideBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height = getHeight();
        int width = getWidth();
        float singleHeight = height * 1.0f / str.length;
        for (int i = 0; i < str.length; i++) {
            mPaint.setColor(Color.WHITE);
            mPaint.setAntiAlias(true);
            mPaint.setTextSize(24);

            float xPos = width / 2 - mPaint.measureText(str[i]) / 2;
            float yPos = singleHeight * i + singleHeight;
            canvas.drawText(str[i], xPos, yPos, mPaint);
            mPaint.reset();
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final float y = event.getY();
        final int oldChoose = choose;
        final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
        final int count = (int) (y / getHeight() * str.length);

        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    setBackground(new ColorDrawable(0x00000000));
                } else {
                    setBackgroundDrawable(new ColorDrawable(0x00000000));
                }
                choose = -1;
                invalidate();
                if (mTextDialog != null) {
                    mTextDialog.setVisibility(View.INVISIBLE);
                }
                break;
            default:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    setBackground(new ColorDrawable(0x13161316));
                } else {
                    setBackgroundDrawable(new ColorDrawable(0x13161316));
                }
                if (oldChoose != count) {
                    if ((count >= 0) && (count < str.length)) {
                        if (listener != null) {
                            listener.onTouchingLetterChanged(str[count]);
                        }
                        if (mTextDialog != null) {
                            mTextDialog.setText(str[count]);
                            mTextDialog.setVisibility(View.VISIBLE);
                        }
                        choose = count;
                        invalidate();
                    }
                }
        }
        return true;
    }
}
